//
//  NSDate+Utilities.m
//  SinaWeather
//
//  Created by suruiqiang on 6/23/11.
//  Copyright 2011 huanghe. All rights reserved.
//

#import "NSDate+Utilities.h"
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
//#define CURRENT_CALENDAR [NSCalendar currentCalendar]
#define CURRENT_CALENDAR [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]


@implementation NSDate (Utilities)


//原串转变成目的串
+(NSString*)convertStringDate:(NSString*)sourceString withFormat:(NSString*)dateFormat fromTimeZone:(NSTimeZone*)sourceTimeZone toTimeZone:(NSTimeZone*)destTimeZone
{
	NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeZone:sourceTimeZone];
	[dateFormatter setDateFormat:dateFormat];
	NSDate* sourceDate = [dateFormatter dateFromString:sourceString];
	
//	NSLog(@"sourceString = %@, [sourceDate description] = %@", sourceString, [sourceDate description]);
	
	
	NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
	NSInteger destGMTOffset = [destTimeZone secondsFromGMTForDate:sourceDate];
	NSTimeInterval interval = destGMTOffset - sourceGMTOffset;
	
	NSDate* destDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
	
	NSString* destString = [dateFormatter stringFromDate:destDate];
	
//	NSLog(@"[destDate description] = %@, destString = %@, ", destString, [destDate description]);
	[destDate release];
	[dateFormatter release];
	
	return destString;
	
	
}


+(NSDate*)dateWithString:(NSString*)dateString withFormat:(NSString*)dateFormat
{
	NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];//guowanlei1 需要改成systemedZone
	[dateFormatter setDateFormat:dateFormat];
    [dateFormatter setCalendar:[[[NSCalendar alloc]
                                initWithCalendarIdentifier:NSGregorianCalendar] autorelease]];
	NSDate* desDate = [dateFormatter dateFromString:dateString];
	[dateFormatter release];
	return desDate;
}

+(NSString*)relativeWithString:(NSString*)dateString
{
    //Tue Jun 05 16:02:53 +0800 2012
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss zzzz yyyy"];
    NSDate *desDate = [dateFormatter dateFromString:dateString];
    [dateFormatter release];
    NSString *newStr = [NSDate relativeFormattedString:desDate];
    return newStr;
}

+ (NSString *)relativeFormattedString:(NSDate*)desData
{
    if (!desData) {
        return @"";
    }
	NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    
	static NSDateFormatter *dateFormatter = nil;
	if (dateFormatter == nil)
	{
		dateFormatter = [[NSDateFormatter alloc] init];
	}
    
	NSDateComponents *nowComponents = [calendar components:unitFlags fromDate:[NSDate date]];
	NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:desData];
	
	[calendar release];
	
	NSString *formattedString = nil;
	if ([nowComponents year] == [dateComponents year] &&
	    [nowComponents month] == [dateComponents month] &&
	    [nowComponents day] == [dateComponents day])				// 今天.
	{
		
		int diff = [desData timeIntervalSinceNow];
        
		if (diff <= 0 && diff > -60 * 60)							// 一小时之内.
		{
			int min = -diff / 60;
			
			if (min == 0)
			{
				min = 1;
			}
            
			if (min <= 1)
			{
				formattedString = [NSString stringWithFormat:@"%d分钟前", min];
			}
			else
			{
				formattedString = [NSString stringWithFormat:@"%d分钟前", min];
			}
		}
		else if (diff > 0)
		{
			formattedString = [NSString stringWithFormat:@"%d分钟前", 1];
		}
		else
		{
			[dateFormatter setDateFormat:@"今天 HH:mm"];
            
			formattedString = [dateFormatter stringFromDate:desData];
		}
	}
	else
	{
		NSLocale *mainlandChinaLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
		[dateFormatter setLocale:mainlandChinaLocale];
		[mainlandChinaLocale release];
		
		if ([nowComponents year] == [dateComponents year])
		{
			[dateFormatter setDateFormat:@"MM-dd HH:mm"];
		}
		else
		{
			[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
		}
		
		formattedString = [dateFormatter stringFromDate:desData];
	}
    
	return formattedString;
}

//test

//rectify 07_27 start 
//当前在self日期之后几日
//都改成当地时区的记时表示
//- (NSInteger) daysAfterOrBeforeDate//:(NSDate *) aDate
//{
//	NSInteger dayInterval;
//		NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];  
//		[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//		[outputFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];  
//		NSString *sysDateStr = [outputFormatter stringFromDate:[NSDate date]];  
//		NSString *pubDateStr = [outputFormatter stringFromDate:self];
//		[outputFormatter release];
//		NSInteger sysDay = [[sysDateStr substringWithRange:NSMakeRange(8, 2)] intValue];
//		NSInteger pubDay = [[pubDateStr substringWithRange:NSMakeRange(8, 2)] intValue];
//	
//		dayInterval = sysDay - pubDay;
//		
//		return dayInterval;
//}
//rectify 07_27 end

/*
//
-(NSString*)getAbbreWeekDay
{
	int interval = [self daysAfterOrBeforeDate];
		switch (interval) {
			case 0:
				return @"今天";
				break;
			case -2:
//			return @"前天";
//				break;
			case -1:
//			return @"昨天";
//				break;
			case 1:
//			return @"明天";
//				break;
			case 2:
			case 3:
			case 4:
			case 5:
				//NSString* weekDay= ;
				return [NSDate getWeekDay:self];
				break;
			default:
				return [self getWeekDay];
				break;
		}
	return nil;
}
*/


//-(NSString*)getUpdateDay
//{
//	int interVal = [self daysAfterOrBeforeDate];
//	if (interVal==1) {
//		return @"昨天";
//	}else 
//	if (interVal==2) {
//		return @"前天";
//	}else
//	if (interVal>2) {
//		return [NSString stringWithFormat:@"%d天前", interVal];
//	}else
//	if (interVal==0) {
//		return @"";
//	}else
//	if (interVal<0) {
//		return @"系统日期错误";
//	}
//	return @"系统日期错误";
//}


+(NSString*)getWeekDay:(NSDate*)date
{
	NSDateComponents *components = [CURRENT_CALENDAR
									components:DATE_COMPONENTS fromDate:date];
	
	NSInteger weekDayIndex = [components weekday];
	switch (weekDayIndex) {
		case 2:
            return @"周一";
            break;
		case 3:
            return @"周二";
            break;
		case 4:
            return @"周三";
            break;
		case 5:
	    return @"周四";
            break;
		case 6:
            return  @"周五";
            break;
		case 7:
            return  @"周六";
            break;
		case 1:
		return @"周日";
            break;
		default:
            return @"  ";
            break;
	}
	
}


-(NSString*)getWeekDay
{
	return [NSDate getWeekDay:self];
}



//得到的年
- (NSInteger) year
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components year];
}

- (NSInteger) day
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components day];
}

- (NSInteger) month
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components month];
}


- (NSInteger) hour
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components hour];
}


- (NSInteger) minute
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components minute];
}

- (NSInteger) second
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components second];
}

- (NSInteger) weekday
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components weekday];
}

@end
